package org.esprit.ocm.client.ui.one;

import org.esprit.ocm.client.rpc.oca.ClusterOcaService;
import org.esprit.ocm.client.rpc.oca.ClusterOcaServiceAsync;
import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;
import org.esprit.ocm.dto.impl.UserDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class CreateCluster extends Window{

	private final ClusterOcaServiceAsync	clusterOcaService	= GWT.create(ClusterOcaService.class);
	
	public CreateCluster(final ServerDto server, final AwsCredentialsDto credentials, final UserDto user) {
		setTitle("Create Cluster");
		setWidth(300);
		setHeight(150);
		setIsModal(true);
		setShowModalMask(true);
		setShowCloseButton(true);
		setShowMinimizeButton(false);
		centerInPage();

		final DynamicForm form1 = new DynamicForm();
		form1.setWidth(250);
		form1.setHeight(20);
		form1.setPadding(5);
		form1.setLayoutAlign(VerticalAlignment.TOP);

		final DynamicForm form2 = new DynamicForm();
		form2.setTitle("Drivers");
		form2.setWidth(250);
		form2.setHeight(20);
		form2.setPadding(20);
		form2.setLayoutAlign(VerticalAlignment.TOP);
		
		VLayout vLayout = new VLayout();
		
		HLayout hLayout1 = new HLayout();
		hLayout1.setWidth("50%");
		HLayout hLayout2 = new HLayout();
		hLayout2.setWidth("50%");

		vLayout.addMember(hLayout1);
		vLayout.addMember(hLayout2);

		final TextItem name = new TextItem("name");
		name.setTitle("name");
		name.setRequired(true);


		ButtonItem createButton = new ButtonItem("create", "Create");
		createButton.setAlign(Alignment.CENTER);
		createButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				
				String secret = credentials.getAwsAccessKeyId() + ":" + credentials.getSecretAccessKey();
				String endpoint = server.getIpServer();
				clusterOcaService.allocate(secret, endpoint, name.getValue().toString(), new AsyncCallback<String>() {
					
					public void onSuccess(String s) {
						destroy();
						SC.warn(s);
					}
					
					public void onFailure(Throwable arg0) {
						// TODO Auto-generated method stub
						
					}
				});
				
			}
		});

		form1.setFields(name);
		form1.redraw();
		form2.setFields(createButton);
		form2.redraw();
		hLayout1.addChild(form1);
		hLayout2.addChild(form2);
		addItem(vLayout);

		draw();

	}

}
